package com.demo.mdmt.impl;

import com.demo.api.user.domain.TestMultiLimitDto;
import com.demo.api.user.service.TestMultiLimitService;
import com.demo.common.enums.ResultCode;
import com.demo.common.response.Result;
import com.demo.mdmt.domain.TestMultiLimit;
import com.demo.mdmt.mapper.TestMultiLimitDao;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 * @since
 */
@Service(version = "1.0.0", interfaceClass = TestMultiLimitService.class, timeout = 120000, retries = 3)
@Slf4j
public class TestMultiLimitServiceImpl implements TestMultiLimitService {

  @Autowired
  TestMultiLimitDao testMultiLimitDao;

  @Override
  public Result queryById(Integer id, Integer libId, Integer tabId) {
    log.info("TestMultiLimitServiceImpl.queryById 执行查询，参数{},{},{}", id,libId,tabId);
    List<com.demo.api.user.domain.TestMultiLimitDto> testMultiLimit = new ArrayList<>(32);
    try {
      Map<String, Object> map = new HashMap<String, Object>(16);
      map.put("id", id);
      map.put("lib_id", libId);
      map.put("tab_id", tabId);
      testMultiLimit = toAipTestLimitList(testMultiLimitDao.selectByMap(map));
    } catch (Exception e){
      log.info("{}", e);
      return Result.fail(ResultCode.SYSTEM_ERROR);
    }
    log.info("TestMultiLimitServiceImpl.queryById 查询正常结束，参数{}", id);
    return Result.suc(testMultiLimit);
  }

  @Override
  @Transactional
  public Result addTestLimit(List<TestMultiLimitDto> testLimitList) {
    log.info("分布式事务, xid:{}", RootContext.getXID());
    log.info("TestMultiLimitServiceImpl.addTestLimit 执行写入，参数{}", testLimitList);
    int result = 0;
    try {
      for (TestMultiLimitDto testMultiLimitDto : testLimitList) {
        TransactionTypeHolder.set(TransactionType.BASE);
        result = testMultiLimitDao.insert(toMdmtTestLimit(testMultiLimitDto));
      }
    } catch (Exception e){
      log.info("{}", e);
      return Result.fail(ResultCode.SYSTEM_ERROR);
    }
    log.info("TestMultiLimitServiceImpl.addTestLimit 执行写入结束");
    return Result.suc(result);
  }

  protected TestMultiLimit toMdmtTestLimit(com.demo.api.user.domain.TestMultiLimitDto testLimit){
    TestMultiLimit testLimitMdmt = new TestMultiLimit();
    BeanUtils.copyProperties(testLimit, testLimitMdmt);
    return testLimitMdmt;
  }

  protected List<com.demo.api.user.domain.TestMultiLimitDto> toAipTestLimitList(List<TestMultiLimit> testMultiLimitList){
    List<com.demo.api.user.domain.TestMultiLimitDto> testMultiLimitDtoList = new ArrayList<>(64);
    for (TestMultiLimit testMultiLimit : testMultiLimitList) {
      com.demo.api.user.domain.TestMultiLimitDto testLimitApi = com.demo.api.user.domain.TestMultiLimitDto.builder().build();
      BeanUtils.copyProperties(testMultiLimit, testLimitApi);
      testMultiLimitDtoList.add(testLimitApi);
    }
    return testMultiLimitDtoList;
  }
}
